package net.yla.smartbus.module.controller.web.ba.right.account;

import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.module.ba.right.account.mapper.AccountsMapper;
import net.yla.smartbus.module.ba.right.account.query.AccountOfQuery;
import net.yla.smartbus.module.ba.right.account.query.ListQuery;
import net.yla.smartbus.module.ba.right.account.service.AccountsService;
import net.yla.smartbus.module.ba.right.admin.query.*;
import net.yla.smartbus.module.ba.right.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @Date: 2020-08-13 15:39
 * @Author: Being Nobody
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/web/account")
public class AccountController extends BaseController {

    @Autowired
    private AccountsService service;



    /*====================================admin======================================*/

    /**
     * 1查询符合条件的记录数
     *
     * @param count
     * @return
     */
    @PostMapping("/count")
    public ApiResult count(@RequestBody AdminCount count) {
        return success(service.count(count));
    }

    /**
     * 2查询符合条件的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/query")
    public ApiResult query(@RequestBody AdminQuery query) {

        /*return success(query,service.account(query));*/

        return success(service.get(query));

    }

    /**
     * 3查看账户记录
     * 主键ID
     *
     * @param query
     * @return
     */
    @PostMapping("/view")
    public ApiResult view(@RequestBody SaveQuery query) {
        return success(service.account(query.getId()));
    }

    /**
     * 4创建或更新账户账号
     *
     * @param query
     * @return
     */
    @PostMapping("/save")
    public ApiResult save(@RequestBody AccountOfQuery query) {
        int i = service.saveSelective(query);
        return success( i);
    }

    /**
     * 4删除用户账户信息
     *
     * @param query
     * @return
     */
    @PostMapping("/del")
    public ApiResult del(@RequestBody SaveQuery query) {
        int del = service.del(query.getId());
        return success(del < 1 ? "更新成功" : "更新失败");
    }

    /**
     * 5批量删除用户账户信息
     *
     * @param query
     * @return
     */
    @PostMapping("/deleteByList")
    public ApiResult deleteByList(@RequestBody ListQuery query) {
        return success(service.deleteByList(query.getAccountIds()));
    }
    /*====================================ip======================================*/

    /**
     * 6获取IP跟踪信息的记录数
     *
     * @param count
     * @return
     */
    @PostMapping("/ip/count")
    public ApiResult ipOfCount(@RequestBody IPCount count) {
        return success(service.ipOfCount(count));
    }

    /**
     * 7获取IP跟踪信息的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/ip/query")
    public ApiResult ipOfQuery(@RequestBody @Validated IPQuery query) {
        return success(service.ipOfQuery(query));
    }

    /**
     * 8查看IP跟踪信息记录
     *
     * @param query
     * @return
     */
    @PostMapping("/ip/view")
    public ApiResult ipOfView(@RequestBody SaveQuery query) {
        return success(service.ipOfView(query.getId()));
    }

    /*====================================log======================================*/

    /**
     * 9获取用户账户登录日志的记录数
     *
     * @param count
     * @return
     */
    @PostMapping("/log/count")
    public ApiResult logOfCount(@RequestBody LogCount count) {
        return success(service.logOfCount(count));
    }

    /**
     * 10查询用户登录日志的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/log/query")
    public ApiResult logOfQuery(@RequestBody @Validated LogQuery query) {
        return success(service.logOfQuery(query));
    }

    /**
     * 11查询用户登录日志记录
     *
     * @param query
     * @return
     */
    @PostMapping("/log/view")
    public ApiResult logOfView(@RequestBody @Validated SaveQuery query) {
        return success(service.logOfView(query.getId()));
    }

    /**
     * 12导出
     *
     * @param query
     * @return
     */
    @PostMapping("/export")
    public void export(AdminQuery query, HttpServletResponse response) {
        service.export(query, response);
    }

    /**
     * 13导出日志
     *
     * @param query
     * @return
     */
    @PostMapping("/log/export")
    public void export(LogQuery query, Long ip, HttpServletResponse response) {
        service.exportLog(query, ip, response);
    }

    /**
     * 14删除账户日志信息
     *
     * @param id
     * @return
     */
    @PostMapping("/log/del")
    public ApiResult delLog(Integer id) {
        int del = service.delLog(id);
        return success(del == 1 ? "更新成功" : "更新失败");
    }

    /**
     * 15批量删除账户日志信息
     *
     * @param ids
     * @return
     */
    @PostMapping("/log/deleteByList")
    public ApiResult deleteByLogList(@RequestParam("ids") List<Integer> ids) {
        return success(service.deleteByLogList(ids));
    }
}
